<?php
class Pagination extends CWidget 
{
	public $pages, $maxButtonCount = 10;
	public function init(){
		if(!isset($this->pages) || !$this->pages) throw new Exception('Invalid pages');
	}
	protected function getPageRange()
	{
		$currentPage=$this->pages->getCurrentPage();
		$pageCount=$this->pages->getPageCount();

		$beginPage=max(0, $currentPage-(int)($this->maxButtonCount/2));
		if(($endPage=$beginPage+$this->maxButtonCount-1)>=$pageCount)
		{
			$endPage=$pageCount-1;
			$beginPage=max(0,$endPage-$this->maxButtonCount+1);
		}

		$beginItem = $this->pages->getCurrentPage()*$this->pages->getPageSize()+1;
		$endItem = ($this->pages->getCurrentPage()+1)*$this->pages->getPageSize();
		if($endItem > $this->pages->getItemCount())
		{
			$endItem = 	$this->pages->getItemCount();
		}
		
		return array($beginPage,$endPage, $beginItem, $endItem);
	}
    public function run(){
		return $this->render('pagination', array('pages'=>$this->pages, 'range'=>$this->getPageRange()));
	}
}